<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="doAdd"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEidet(row)">
                    编辑
                  </el-button>
                  <el-button size="small" type="danger" @click="doDel(row.id)">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="formData.page"
                :page-sizes="[2, 3, 4, 5]"
                :page-size="2"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="isEidet ? '编辑角色' : '添加角色'"
        :visible.sync="showDialog"
      >
        <el-form
          ref="formRef"
          label-width="100px"
          :rules="rules"
          :model="roleForm"
        >
          <el-form-item label="添加角色:" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述:" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="doSubmit">确定</el-button>
            <el-button type="danger" @click="showDialog = false">
              取消
            </el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getRole,
  addRoles,
  delRoles,
  getRolesDetail,
  editRoles
} from '@/api/settings'
export default {
  data() {
    return {
      isEidet: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      showDialog: false,
      list: [],
      formData: {
        page: 1,
        pagesize: 2
      },
      total: 0
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    async loadeditRoles() {
      const res = await editRoles(this.roleForm)
      console.log(res)
      this.loadRole()
    },
    async doEidet(row) {
      this.roleForm = { ...row }
      this.isEidet = true
      this.showDialog = true
      const res = await getRolesDetail(row.id)
      console.log(res)
      this.roleForm = res.data
    },

    hClose() {
      this.$refs.formRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    async doDel(id) {
      const res = await delRoles(id)
      console.log(res)
      if (this.list.length === 1) {
        this.formData.page--
        if (this.formData.page <= 0) {
          this.formData.page = 1
        }
      }
      this.loadRole()
    },
    doSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return
        this.showDialog = false
        this.isEidet ? this.loadeditRoles() : this.loadaddRoles()
      })
    },
    doAdd() {
      this.isEidet = false
      this.showDialog = true
    },
    async loadaddRoles() {
      const res = await addRoles(this.roleForm)
      console.log(res)
      this.total++
      this.formData.page = Math.ceil(this.total / this.formData.pagesize)
      this.loadRole()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.formData.pagesize = val
      this.loadRole()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.formData.page = val
      this.loadRole()
    },
    async loadRole() {
      const res = await getRole(this.formData)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
